package warehouse;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import javax.swing.JButton;
import javax.swing.JFrame;

import javax.swing.JTextArea;

public class Gui extends JFrame implements ActionListener, Observer {

	private static final long serialVersionUID = 1L;
	private OrderList allOrders;
	private ItemList allItems;
	private Mouse mousedata;

	JButton start, stop;
	static JTextArea displayList1;
	static JTextArea displayList2;
	static JTextArea displayOrders;
	static JTextArea displayItems;

	public Gui(OrderList orders, ItemList items) {
		setTitle("Warehouse");
		allOrders = orders;
		allItems = items;

		// ms.registerObserver(this);
		setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
		setupEastPanel();
		setupNorthPanel();
		setupCenterPanel();
		setupWestPanel();

		pack();
		setVisible(true);

	}

	// implementation of Center Panel
	private void setupCenterPanel() {
		// components// displayList,scrollList
		JPanel center = new JPanel();
		center.setLayout(new GridLayout(2, 1));
		displayList1 = new JTextArea(10, 30);
		displayList1.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
		displayList1.setEditable(false);

		displayList2 = new JTextArea(10, 30);
		displayList2.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
		displayList2.setEditable(false);
		center.add(displayList1);
		center.add(displayList2);
		this.add(center, BorderLayout.CENTER);

	}

	// implementation of East Panel
	private void setupEastPanel() {

		JPanel east = new JPanel();
		displayOrders = new JTextArea(10, 30);
		displayOrders.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
		displayOrders.setEditable(false);
		String orders = "LIST OF ORDERS\n";
		orders += allOrders.listDetails();

		displayOrders.setText(orders);
		east.add(displayOrders);

		this.add(east, BorderLayout.EAST);

	}

	// implementation of West Panel
	private void setupWestPanel() {

		JPanel west = new JPanel();
		displayItems = new JTextArea(10, 30);
		displayItems.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 11));
		displayItems.setEditable(false);

		String items = "WAREHOUSE\n";
		items += allItems.printItems();

		displayItems.setText(items);

		west.add(displayItems);

		this.add(west, BorderLayout.WEST);

	}

	// implementation of North Panel
	private void setupNorthPanel() {
		// components: comNum, compName
		JPanel north = new JPanel();
		start = new JButton("START DAY");
		start.addActionListener(this);
		stop = new JButton("END DAY");
		stop.addActionListener(this);
		north.add(start);
		north.add(stop);
		this.add(north, BorderLayout.NORTH);

	}

	// define what the buttons do for us
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == start)
			process();
		// Worker w= new Worker;

		// if we press search we implement the search method
		// if (e.getSource() == stop)
		// if we press the update button we implement the update method

	}

	public void process() {

		Thread thread1 = new Thread(new Worker(allOrders, allItems, 1));
		thread1.start();
		Thread thread2 = new Thread(new Worker(allOrders, allItems, 2));
		thread2.start();

	}

	public void update() {
		System.out.println(mousedata + "SKATA");
	}

	public static void setDisplay(String order, int w) {
		if (w == 1) {
			displayList1.setText(order);
		}
		if (w == 2) {
			displayList2.setText(order);
		}
	}

}
